package io.finer.erp.jeecg.purchase.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.finer.erp.jeecg.approve.entity.ApproveRecord;
import io.finer.erp.jeecg.purchase.entity.PurchaseRequest;
import io.finer.erp.jeecg.purchase.entity.PurchaseRequestLine;
import io.finer.erp.jeecg.purchase.service.PurchaseRequestLineService;
import io.finer.erp.jeecg.purchase.service.PurchaseRequestService;
import io.finer.erp.jeecg.purchase.vo.PurchaseRequestPage;
import io.finer.erp.jeecg.stock.entity.StkCheckBill;
import io.finer.erp.jeecg.stock.entity.StkCheckBillEntry;
import io.finer.erp.jeecg.stock.vo.StkCheckBillPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 采购申请 前端控制器
 * </p>
 *
 * @author yaoyifan
 * @since 2021-04-12
 */
@Api(tags="采购申请")
@RestController
@RequestMapping("/purchase/purchaseRequest")
@Slf4j
public class PurchaseRequestController {
    @Autowired
    PurchaseRequestService purchaseRequestService;

    @Autowired
    PurchaseRequestLineService purchaseRequestLineService;

    @AutoLog(value="采购申请-分页列表查询")
    @ApiOperation(value="采购申请-分页列表查询", notes = "采购申请-分页列表查询")
    @GetMapping("/list")
    public Result<?> queryPageList(PurchaseRequestPage purchaseRequestPage,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req){
        QueryWrapper<PurchaseRequestPage> queryWrapper = QueryGenerator.initQueryWrapper(purchaseRequestPage, req.getParameterMap());
        Page<PurchaseRequest> page = new Page<>(pageNo, pageSize);
        IPage<PurchaseRequestPage> pageList = purchaseRequestService.selectPurchaseRequestPage(page, queryWrapper);
        return Result.ok(pageList);
    }

    @AutoLog(value = "采购申请-添加")
    @ApiOperation(value="采购申请-添加", notes="采购申请-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody PurchaseRequestPage purchaseRequestPage) {
        PurchaseRequest purchaseRequest = new PurchaseRequest();
        BeanUtils.copyProperties(purchaseRequestPage, purchaseRequest);
        purchaseRequestService.saveMain(purchaseRequest, purchaseRequestPage.getPurchaseRequestLinelist());
        return Result.ok("添加成功！");
    }

    @AutoLog(value = "采购申请-编辑")
    @ApiOperation(value="采购申请-编辑", notes="采购申请-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody PurchaseRequestPage purchaseRequestPage) {
        PurchaseRequest purchaseRequest = new PurchaseRequest();
        BeanUtils.copyProperties(purchaseRequestPage, purchaseRequest);
        PurchaseRequest purchaseRequestEntity = purchaseRequestService.getById(purchaseRequest.getId());
        if(purchaseRequestEntity==null) {
            return Result.error("未找到对应数据");
        }
        purchaseRequestService.updateMain(purchaseRequest, purchaseRequestPage.getPurchaseRequestLinelist());
        return Result.ok("编辑成功!");
    }

    @AutoLog(value = "采购申请-删除")
    @ApiOperation(value="采购申请-删除", notes="采购申请-删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id", required = true) String id){
        this.purchaseRequestService.delMain(id);
        return Result.ok("删除成功!");
    }


    @AutoLog(value = "采购申请-批量删除")
    @ApiOperation(value="采购申请-批量删除", notes="采购申请-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.purchaseRequestService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    @AutoLog(value = "采购申请-通过id查询")
    @ApiOperation(value="采购申请-通过id查询", notes="采购申请-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        PurchaseRequestPage purchaseRequestPage = purchaseRequestService.selectPurchaseRequestPageById(id);
        if(purchaseRequestPage==null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(purchaseRequestPage);
    }

//    @AutoLog(value = "库存盘点单-通过id审核")
//    @ApiOperation(value="库存盘点单-通过id审核", notes="库存盘点单-通过id审核")
//    @PutMapping(value = "/approve")
//    public Result<?> approve(@RequestBody JSONObject json) {
//        stkCheckBillService.approve(json.getString("id"));
//        return Result.ok("审核通过!");
//    }
    @AutoLog(value="采购申请-提交")
    @ApiOperation(value="采购申请-提交", notes="采购申请-通过id提交生成审批流")
    @PutMapping(value = "/submit")
    public Result<?> submit(@RequestParam(name="id",required=true) String id){
        System.out.println("开始提交");
        purchaseRequestService.submit("purchase_request", id);

        return Result.ok("提交成功!");
    }

    @AutoLog(value="采购申请-审批")
    @ApiOperation(value="采购申请-审批", notes="采购申请-通过id审批")
    @PutMapping(value = "/approve")
    public Result<?> approve(@RequestBody JSONObject json){
        purchaseRequestService.approve("purchase_request",json.getString("id"),json.getString("note"));
        return Result.ok("审批成功!");
    }

    @AutoLog(value="采购申请-拒绝")
    @ApiOperation(value="采购申请-拒绝", notes="采购申请-通过id拒绝")
    @PutMapping(value = "/reject")
    public Result<?> reject(@RequestBody JSONObject json){
        purchaseRequestService.reject("purchase_request",json.getString("id"),json.getString("note"));
        return Result.ok("拒绝成功!");
    }

    @AutoLog(value="采购申请-撤回")
    @ApiOperation(value="采购申请-撤回", notes="采购申请-通过id撤回-只有已提交状态可以撤回")
    @PutMapping(value = "/withdraw")
    public Result<?> withdraw(@RequestParam(name="id",required=true) String id){
        purchaseRequestService.withDraw("purchase_request",id);
        return Result.ok("撤回成功!");
    }

    @AutoLog(value="采购申请-作废")
    @ApiOperation(value="采购申请-作废", notes="采购申请-通过id作废")
    @PutMapping(value = "/invalid")
    public Result<?> withdraw(@RequestBody JSONObject json){
        purchaseRequestService.invalid(json.getString("id"), json.getString("note"));
        return Result.ok("作废成功!");
    }


    @AutoLog(value="采购申请-按钮操作权限")
    @ApiOperation(value="采购申请-按钮操作权限", notes="采购申请-按钮操作权限")
    @GetMapping(value = "/canOperates")
    public Result<?> canOperates(@RequestParam(name="id",required=true) String id){
        boolean canApproveorreject = purchaseRequestService.canApproveorReject("purchase_request", id);
        Map<String, Boolean> otherOperates = purchaseRequestService.canSubmitorWithdraworInvalid("purchase_request", id, this.purchaseRequestService);
        JSONObject result = new JSONObject();
        result.putAll(otherOperates);
        result.put("canApproveorReject", canApproveorreject);
        return Result.ok(result);
    }

    @AutoLog(value="采购申请-查看审批记录")
    @ApiOperation(value="采购申请-查看审批记录", notes="采购申请-查看审批记录")
    @GetMapping(value = "/queryApproveRecords")
    public Result<?> queryApproveRecords(@RequestParam(name="id",required=true) String id){
        List<ApproveRecord> approveRecordList = purchaseRequestService.queryApproveRecordList("purchase_request", id);
        return Result.ok(approveRecordList);
    }

}

